package cn.fl.riskctrl.service.impl;

import cmm.comm.facade.framework.api.ISysCtrlCfgApiService;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.service.IPrjBscInfoService;
import cn.fl.project.service.IPrjCstBscInfoService;
import cn.fl.riskctrl.service.IRskCtrlRecService;
import fl.constant.facade.framework.constant.SysCtrlCfgConstant;
import fl.constant.facade.framework.constant.SystemConstant;
import fl.customer.facade.customer.api.ICustomerApiService;
import fl.customer.facade.customer.api.ICustomerQueryApiService;
import fl.riskctrl.facade.riskctrl.api.IRiskCtrlApiService;
import fl.riskctrl.facade.riskctrl.condition.RskCtrlIsCorrectQC;
import fl.riskctrl.facade.riskctrl.dto.DataResultDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 风控服务接口实现
 * @author FUJINGRUN
 */
@Service
public class RskCtrlRecServiceImpl implements IRskCtrlRecService {

	private static final Logger logger = LoggerFactory.getLogger(RskCtrlRecServiceImpl.class);

	/**风控API服务*/
	@Autowired
	public IRiskCtrlApiService riskCtrlApiService;
	/**项目服务*/
	@Autowired
	public IPrjBscInfoService prjBscInfoService;
	/**客户服务*/
	@Autowired
	public ICustomerApiService customerApiService;
	/**项目客户服务*/
	@Autowired
	public IPrjCstBscInfoService prjCstBscInfoService;
	/**客户服务*/
	@Autowired
	public ICustomerQueryApiService customerQueryApiService;
	/**系统配置服务*/
	@Autowired
	public ISysCtrlCfgApiService sysCtrlCfgApiService;

//	@Override
//	public DataResultDTO getRiskCtrlIsCorrect(RskCtrlIsCorrectQC qc) throws BaseException {
//		String loadAllCd = PropertiesCacheUtils.getProperty(SysCtrlCfgConstant.RISK_CTRL_LOAD_ALL_SWITCH_CD);
//		Map<String, String> map = this.getSwitch(loadAllCd);
//		qc.setSysCd(CurrentThreadContext.getFlServiceSysCd());
//		qc.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
//		return this.riskCtrlApiService.getRiskCtrlIsCorrect(qc, Boolean.valueOf(map.get(loadAllCd)));
//	}
	
	@Override
	public DataResultDTO checkPYJyIsCorrect(RskCtrlIsCorrectQC qc) throws BaseException {
		String loadAllCd = PropertiesCacheUtils.getProperty(SysCtrlCfgConstant.RISK_CTRL_LOAD_ALL_SWITCH_CD);
		Map<String, String> map = this.getSwitch(loadAllCd);
		qc.setSysCd(CurrentThreadContext.getFlServiceSysCd());
		qc.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
		return this.riskCtrlApiService.checkPYJyIsCorrect(qc, Boolean.valueOf(map.get(loadAllCd)));
	}
	
	@Override
	public DataResultDTO checkPYJzIsCorrect(RskCtrlIsCorrectQC qc) throws BaseException {
		String loadAllCd = PropertiesCacheUtils.getProperty(SysCtrlCfgConstant.RISK_CTRL_LOAD_ALL_SWITCH_CD);
		Map<String, String> map = this.getSwitch(loadAllCd);
		qc.setSysCd(CurrentThreadContext.getFlServiceSysCd());
		qc.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
		return this.riskCtrlApiService.checkPYJzIsCorrect(qc, null, Boolean.valueOf(map.get(loadAllCd)));
	}

	@Override
	public DataResultDTO checkKJYIsCorrect(RskCtrlIsCorrectQC qc) throws BaseException {
		qc.setSysCd(CurrentThreadContext.getFlServiceSysCd());
		qc.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
		return this.riskCtrlApiService.checkKJYIsCorrect(qc);
	}

	@Override
	public DataResultDTO checkSYSIsCorrect(RskCtrlIsCorrectQC qc) throws BaseException {
		qc.setSysCd(CurrentThreadContext.getFlServiceSysCd());
		qc.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
		return this.riskCtrlApiService.checkSYSIsCorrect(qc);
	}

	@Override
	public DataResultDTO checkPyEduIsCorrect(RskCtrlIsCorrectQC qc) throws BaseException {
		qc.setSysCd(CurrentThreadContext.getFlServiceSysCd());
		qc.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
		return this.riskCtrlApiService.checkPyEduIsCorrect(qc);
	}

	@Override
	public DataResultDTO checkJXLIsCorrect(RskCtrlIsCorrectQC qc) throws BaseException {
		qc.setSysCd(CurrentThreadContext.getFlServiceSysCd());
		qc.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
		return this.riskCtrlApiService.checkJXLIsCorrect(qc);
	}

	@Override
	public DataResultDTO checkBrIsCorrect(RskCtrlIsCorrectQC qc) throws BaseException {
		qc.setSysCd(CurrentThreadContext.getFlServiceSysCd());
		qc.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
		return this.riskCtrlApiService.checkBrIsCorrect(qc);
	}

	@Override
	public DataResultDTO checkBrblIsCorrect(RskCtrlIsCorrectQC qc) throws BaseException {
		qc.setSysCd(CurrentThreadContext.getFlServiceSysCd());
		qc.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
		return this.riskCtrlApiService.checkBrblIsCorrect(qc);
	}

	@Override
	public DataResultDTO checkBrPersonIsCorrect(RskCtrlIsCorrectQC qc) throws BaseException {
		qc.setSysCd(CurrentThreadContext.getFlServiceSysCd());
		qc.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
		return this.riskCtrlApiService.checkBrPersonIsCorrect(qc);
	}

	@Override
	public DataResultDTO checkTDIsCorrect(RskCtrlIsCorrectQC qc) throws BaseException {
		qc.setSysCd(CurrentThreadContext.getFlServiceSysCd());
		qc.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
		return this.riskCtrlApiService.checkTDIsCorrect(qc);
	}

	@Override
	public DataResultDTO checkZZCIsCorrect(RskCtrlIsCorrectQC qc) throws BaseException {
		qc.setSysCd(CurrentThreadContext.getFlServiceSysCd());
		qc.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
		return this.riskCtrlApiService.checkZZCIsCorrect(qc);
	}
	
	@Override
	public DataResultDTO checkXjIsCorrect(RskCtrlIsCorrectQC qc) throws BaseException {
		qc.setSysCd(CurrentThreadContext.getFlServiceSysCd());
		qc.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
		return this.riskCtrlApiService.checkXjIsCorrect(qc);
	}
	
	
	//*********************************************设置担保人数据*****************************************//
	/**获取开关*/
	private Map<String, String> getSwitch(String loadAllCd){
		String defOrgCd = PropertiesCacheUtils.getProperty(SystemConstant.FL_ORG_CD);
		//获取总开关, 如果关闭则按照正常情况返回
		List<String> ctrlCfgCodes = new ArrayList<>();
		ctrlCfgCodes.add(loadAllCd);
		Map<String, String> map = this.sysCtrlCfgApiService.searchSysCtrlCfgByCtrlCodes(ctrlCfgCodes, CurrentThreadContext.getFlServiceSysCd(), defOrgCd);
		//获取是否全部加载数据开关
		if(CollectionUtils.isMapEmpty(map)){
			map = new HashMap<>();
		}
		return map;
	}
}
